package com.hmall.trade.listen;

import com.hmall.trade.domain.po.Order;
import com.hmall.trade.service.IOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class PayStatusListener {
    private final IOrderService orderService;
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "pay.success.order.queue",durable = "true"),
            exchange = @Exchange(value = "pay.direct",type = ExchangeTypes.DIRECT),
            key = {"success"}))
    public void listenPaySuccess(Long orderId){
        //1.先查询订单
        Order order = orderService.getById(orderId);
        //2.判断状态为 未支付
        if(order!=null && order.getStatus().equals(1)){
            //设置状态已支付
            orderService.markOrderPaySuccess(orderId);
        }
    }
}
